﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using GSQ.BusinessEntities;
using GSQ.BusinessLogic;
using GSQ.Rpt_s;
using UIapp.Interfaces;

namespace UIapp.Vistas
{
    public partial class frmCotizaciones : Plantillas.frmViewPlantilla, IPantalla
    {
        public frmCotizaciones()
        {
            InitializeComponent();
            this.entityCollection = new List<COTIZACIONES>();
            this.anularFacturaToolStripMenuItem.Image = new System.Drawing.Bitmap(Comun.systemConfig.RUTA_IMAGENES + "NullReport.png");
            this.reImprimirToolStripMenuItem.Image = new System.Drawing.Bitmap(Comun.systemConfig.RUTA_IMAGENES + "printer.png");
            this.verEnPantallaToolStripMenuItem.Image = new System.Drawing.Bitmap(Comun.systemConfig.RUTA_IMAGENES + "printDisplay.png");
            this.dtpFechaFinal.Value = DateTime.Now;
            this.dtpFechaInicial.Value = DateTime.Now;
        }

        #region Variables
        List<COTIZACIONES> entityCollection;
        #endregion

        #region Implementacion IPantalla
        public int ComandosSoportados
        {
            get
            {
                return (int)ComandosPantalla.Buscar |
                       (int)ComandosPantalla.Nuevo;
            }
        }

        public void Nuevo()
        {
            Dialogos.frmCotizaciones frm = new Dialogos.frmCotizaciones(null);
            frm.Owner = this;
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        public void Modificar()
        {
            if (this.bindingSource.Count > 0)
            {
                Dialogos.frmFacturas frm = new Dialogos.frmFacturas(new FacturasBL().GetById
                    (
                        ((FACTURAS)this.radGridView.CurrentRow.DataBoundItem).ID)
                    );
                frm.MdiParent = this.MdiParent;
                frm.Text = frm.Text + " " + ((FACTURAS)this.radGridView.CurrentRow.DataBoundItem).ID.ToString("000000");
                frm.Show();
            }
        }

        public void Eliminar()
        {
            throw new NotImplementedException();
        }

        public void Refrescar()
        {
            this.Buscar();
        }

        public void Buscar()
        {
            if (this.chkPorFechas.IsChecked)
            {
                if (dtpFechaInicial.Value.Date > dtpFechaFinal.Value.Date)
                {
                    Comun.ShowInMainMessage("Rango de fecha no valido.");
                    return;
                }
            }

            if (this.chkPorID.IsChecked && this.txtSearchValue.Text.Trim() != string.Empty)
            {
                decimal id = 0;
                if (!decimal.TryParse(this.txtSearchValue.Text.Trim(), out id))
                {
                    Comun.ShowInMainMessage("Solo valores numéricos para esta busqueda.");
                    return;
                }
            }

            if (this.chkPorNroFactura.IsChecked && this.txtSearchValue.Text.Trim() != string.Empty)
            {
                decimal id = 0;
                if (!decimal.TryParse(this.txtSearchValue.Text.Trim(), out id))
                {
                    Comun.ShowInMainMessage("Solo valores numéricos para esta busqueda.");
                    return;
                }
            }
            this.backgroundWorker.RunWorkerAsync();
            Comun.ShowLoadingFRM();
        }

        public void AgregarProducto(Controles.TransferEntity tranferEntity)
        {
            throw new NotImplementedException();
        }
        #endregion

        #region Tratamiento de eventos
        private void txtBuscar_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                this.Buscar();
                e.Handled = true;
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Listar();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else
            {
                this.bindingSource.DataSource = this.entityCollection.ToList();
            }
        }

        private void chkPorID_Click(object sender, EventArgs e)
        {
            this.pnlBuscar.Visible = true;
            this.pnlFechas.Visible = false;
        }

        private void chkPorFechas_Click(object sender, EventArgs e)
        {
            this.pnlBuscar.Visible = false;
            this.pnlFechas.Visible = true;
        }

        private void frmFacturas_Load(object sender, EventArgs e)
        {
            this.dtpFechaFinal.Value = DateTime.Now;
            this.dtpFechaInicial.Value = DateTime.Now;
        }

        private void anularFacturaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Dialogos.frmFacturasAnular((FACTURAS)this.radGridView.CurrentRow.DataBoundItem).ShowDialog();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            if (this.entityCollection.Count == 0)
            {
                e.Cancel = true;
                return;
            }
            if (((COTIZACIONES)this.radGridView.CurrentRow.DataBoundItem).ACTIVO)
                this.anularFacturaToolStripMenuItem.Enabled = true;
            else
                this.anularFacturaToolStripMenuItem.Enabled = false;
        }

        private void reImprimirToolStripMenuItem_Click(object sender, EventArgs e)
        {

            new Reportes().PrintCotizacion(((COTIZACIONES)this.radGridView.CurrentRow.DataBoundItem).ID, this.chkShowCodReference.Checked);

        }

        private void verEnPantallaToolStripMenuItem_Click(object sender, EventArgs e)
        {

            Plantillas.frmReportPreview frm = new Plantillas.frmReportPreview();
            frm.crystalReportViewer1.ReportSource = new Reportes().GetCotizacion(((COTIZACIONES)this.radGridView.CurrentRow.DataBoundItem).ID, this.chkShowCodReference.Checked);
            // frm.MdiParent = this.MdiParent;
            frm.Show();
        }
        #endregion

        #region Metodos auxiliares
        public void Listar()
        {
            this.entityCollection.Clear();
            decimal id = 0;
            if (!this.chkPorFechas.IsChecked)
            {
                if (this.txtSearchValue.Text.Trim() == string.Empty)
                {
                    if (MessageBox.Show("¿Desea listar todos los registros? Esto puede tardar tiempo considerado.",

                        "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.entityCollection = new CotizacionesBL().GetAll();
                    }
                    else
                    {
                        this.txtSearchValue.Focus();
                    }
                    return;
                }
                if (this.chkPorID.IsChecked)
                {
                    if (decimal.TryParse(this.txtSearchValue.Text.Trim(), out id))
                        this.entityCollection.Add(new CotizacionesBL().GetById(id));
                }
                else if (this.chkPorCliente.IsChecked)
                {
                    if (decimal.TryParse(this.txtSearchValue.Text.Trim(), out id))
                    {
                        this.entityCollection = new CotizacionesBL().GetByIdCliente(id);
                    }
                    else
                    {
                        this.entityCollection = new CotizacionesBL().GetByCliente(this.txtSearchValue.Text.Trim());
                    }
                }
            }
            else
            {
                this.entityCollection = new CotizacionesBL().GetByFechas(dtpFechaInicial.Value.Date, dtpFechaFinal.Value.Date);
            }
        }
        #endregion
    }
}

